<template>
  <div class="ele-body">
    <a-row :gutter="12">
      <a-col :style="{ width: width300 ? '300px' : '400px' }">
        <org-tree
          @treeSelect="treeSelect"
          ref="orgTreeRef"
          isShowEditIcon
          @deleteOrg="reload"
          @addOrg="addOrg"
          @editOrg="editOrg"
          @changeWidth="changeWidth"
        ></org-tree>
      </a-col>
      <a-col :style="{ width: width300 ? 'calc(100% - 300px)' : 'calc(100% - 400px)' }">
        <div class="ten-height bg-white padding10 box-shadow">
          <div class="table-header">
            <div class="header-left">
              <a-space size="large">
                <a-input
                  v-model:value="where.searchText"
                  placeholder="机构名称、编码（回车搜索）"
                  @pressEnter="reload"
                  class="search-input"
                >
                  <template #suffix>
                    <search-outlined @click="reload" />
                  </template>
                </a-input>
                <drop-down :list="statusList" defaultName="状态" ref="statusRef" keyValue="id" keyName="name" @dropClick="statusChange" />
                <a-button type="primary" style="background-color: red; border-color: red" class="border-radius" @click="clear"
                  >清空条件</a-button
                >
              </a-space>
            </div>
            <div class="header-right">
              <a-space>
                <a-button type="primary" class="border-radius" @click="openAddEdit()" v-permission="['ADD_ORGDATA']"
                  ><plus-outlined />新建</a-button
                >
                <a-dropdown>
                  <template #overlay>
                    <a-menu @click="moreClick">
                      <a-menu-item key="1">
                        <ordered-list-outlined />
                        自定义列
                      </a-menu-item>
                      <div v-permission="['DELETE_ORGDATA']">
                        <a-menu-item key="2">
                          <delete-outlined style="color: red" />
                          删除
                        </a-menu-item>
                      </div>
                    </a-menu>
                  </template>
                  <a-button>
                    更多
                    <small-dash-outlined />
                  </a-button>
                </a-dropdown>
              </a-space>
            </div>
          </div>
          <div class="table-content">
            <common-table
              :columns="columns"
              :where="where"
              bordered
              rowId="orgId"
              ref="tableRef"
              url="/orgdata/page"
              :scroll="{ x: 'max-content', y: 'calc(100vh - 240px)' }"
            >
              <template #bodyCell="{ column, record }">
                <!-- 姓名 -->
                <template v-if="column.dataIndex == 'orgName'">
                  <a @click="detail(record)">{{ record.orgName }}</a>
                </template>
                <!-- 机构状态 -->
                <template v-if="column.dataIndex == 'statusFlag'">
                  <a-tag color="green" v-if="record.statusFlag == 1">启用</a-tag>
                  <a-tag color="red" v-if="record.statusFlag == 2">禁用</a-tag>
                </template>
                <!-- <template v-if="column.dataIndex == 'orgType'">
                  <div class="org-type">
                    <i class="iconfont icon-org-tree-company" v-if="record.orgType == 1"></i>
                    <i class="iconfont icon-org-tree-dept" v-if="record.orgType == 2"></i>
                    <a-tag color="green" v-if="record.orgType == 1">公司</a-tag>
                    <a-tag color="red" v-if="record.orgType == 2">部门</a-tag>
                  </div>
                </template> -->
                <!-- 操作 -->
                <template v-if="column.key == 'action'">
                  <a-space>
                    <icon-font
                      type="class"
                      iconClass="icon-button-edit"
                      color="var(--primary-color)"
                      font-size="20px"
                      fontWeight="bold"
                      title="编辑"
                      v-permission="['EDIT_ORG']"
                      @click="openAddEdit(record)"
                    ></icon-font>
                    <icon-font
                      type="class"
                      iconClass="icon-button-delete"
                      color="red"
                      font-size="20px"
                      fontWeight="bold"
                      title="删除"
                      v-permission="['DELETE_ORG']"
                      @click="remove(record)"
                    ></icon-font>
                    <!-- <icon-font
                      type="class"
                      iconClass="icon-menu-quanxiankongzhi"
                      color="var(--primary-color)"
                      font-size="20px"
                      fontWeight="bold"
                      title="审批人设置"
                      v-permission="['ASSIGN_APPROVER']"
                      @click="setApprover(record)"
                    ></icon-font> -->
                  </a-space>
                </template>
              </template>
            </common-table>
          </div>
        </div>
      </a-col>
    </a-row>

    <!-- 自定义列 -->
    <Custom
      v-model:visible="isShowCustom"
      v-if="isShowCustom"
      :data="columns"
      @done="val => (columns = val)"
      :fieldBusinessCode="fieldBusinessCode"
    />

    <!-- 新增编辑弹框 -->
    <OrgAddEdit v-model:visible="showEdit" v-if="showEdit" :data="current" @done="reloadAll" :parentId="parentId" :parentName="parentName"/>
    <!-- 查看详情抽屉 
    <OrgDetail v-model:visible="showDetail" v-if="showDetail" :data="current" :isShowApprover="isShowApprover" />-->
  </div>
</template>

<script setup name="Organization">
import OrgTree from '@/views/system/structure/user/orgdata-tree.vue';
import { OrgApi } from './api/OrgApi';
import { ref, createVNode, onMounted } from 'vue';
import { message, Modal } from 'ant-design-vue/es';
import OrgDetail from './components/org-detail.vue';
import OrgAddEdit from './components/org-add-edit.vue';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { CustomApi } from '@/components/common/Custom/api/CustomApi';

// 状态列表
const statusList = ref([
  {
    id: '',
    name: '全部状态'
  },
  {
    id: 1,
    name: '启用'
  },
  {
    id: 2,
    name: '禁用'
  }
]);
// 表格配置
const columns = ref([
  {
    key: 'index',
    title: '序号',
    width: 48,
    align: 'center',
    isShow: true,
    hideInSetting: true,
    customRender: ({ index }) => tableRef.value.tableIndex + index
  },
  {
    dataIndex: 'orgName',
    title: '机构名称',
    align: 'center',
    ellipsis: true,
    width: 200,
    isShow: true
  },
  {
    dataIndex: 'orgCode',
    title: '机构编码',
    width: 100,
    align: 'center',
    isShow: true
  },
  {
    dataIndex: 'statusFlag',
    title: '机构状态',
    align: 'center',
    ellipsis: true,
    width: 100,
    isShow: true
  },
  // {
  //   dataIndex: 'orgType',
  //   title: '机构类型',
  //   align: 'center',
  //   ellipsis: true,
  //   width: 100,
  //   isShow: true
  // },
  {
    dataIndex: 'orgSort',
    title: '排序',
    align: 'center',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'createTime',
    title: '创建时间',
    align: 'center',
    width: 150,
    isShow: true
  },
  {
    key: 'action',
    title: '操作',
    align: 'center',
    width: 100,
    isShow: true
  }
]);
// ref
const tableRef = ref(null);
const statusRef = ref(null);
const orgTreeRef = ref(null);
// 宽度是否300
const width300 = ref(true);

// 搜索条件
const where = ref({
  orgId: null,
  statusFlag: '',
  searchText: ''
});
// 是否显示自定义列
const isShowCustom = ref(false);
// 当前行数据
const current = ref(null);
// 是否显示新增编辑弹框
const showEdit = ref(false);
// 是否显示详情抽屉
const showDetail = ref(false);
// 当前选中的组织机构id
const parentId = ref(null);
// 当前选中的组织机构名称
const parentName = ref(null);
// 业务标识的编码
const fieldBusinessCode = ref('ORG_TABLE');
// 是否显示设置审批人
//const isShowApprover = ref(false);

onMounted(() => {
  getColumnData();
});

// 获取表格配置
const getColumnData = () => {
  CustomApi.getUserConfig({ fieldBusinessCode: fieldBusinessCode.value }).then(res => {
    if (res.tableWidthJson) {
      columns.value = JSON.parse(res.tableWidthJson);
    }
  });
};

// 左侧树选中
const treeSelect = (selectedKeys, metadata) => {
  where.value.orgId = selectedKeys[0];
  parentId.value = selectedKeys[0];
  parentName.value = metadata.node.orgName;
  reload();
};

// 更多点击
const moreClick = ({ key }) => {
  if (key == '1') {
    isShowCustom.value = true;
  } else if (key == '2') {
    batchDelete();
  }
};

// 刷新全部
const reloadAll = () => {
  orgTreeRef.value.reloadOrgTreeData();
  reload();
};

// 点击搜索
const reload = () => {
  tableRef.value.reload();
};

// 清除搜索条件
const clear = () => {
  where.value.searchText = '';
  where.value.statusFlag = '';
  where.value.orgId = null;
  orgTreeRef.value.currentSelectKeys = [];
  statusRef.value.changeDropname('状态');
  reload();
};

// 状态改变
const statusChange = val => {
  where.value.statusFlag = val;
  reload();
};

// 新增编辑点击
const openAddEdit = record => {
  if (!where.value.orgId) {
    parentName.value = '';
    parentId.value = '';
  }
  current.value = record;
  showEdit.value = true;
};

// 查看详情
const detail = record => {
  //isShowApprover.value = false;
  current.value = record;
  showDetail.value = true;
};

// 设置审批人
// const setApprover = record => {
//   isShowApprover.value = true;
//   current.value = record;
//   showDetail.value = true;
// };

// 删除单个
const remove = record => {
  Modal.confirm({
    title: '提示',
    content: '确定要删除选中的组织机构吗?',
    icon: createVNode(ExclamationCircleOutlined),
    maskClosable: true,
    onOk: async () => {
      const res = await OrgApi.delete({ orgId: record.orgId });
      message.success(res.message);
      reload();
    }
  });
};

// 批量删除
const batchDelete = () => {
  if (tableRef.value.selectedRowList && tableRef.value.selectedRowList.length == 0) {
    return message.warning('请选择需要删除的组织机构');
  }
  Modal.confirm({
    title: '提示',
    content: '确定要删除选中的组织机构吗?',
    icon: createVNode(ExclamationCircleOutlined),
    maskClosable: true,
    onOk: async () => {
      const res = await OrgApi.batchDelete({ orgIdList: tableRef.value.selectedRowList });
      message.success(res.message);
      reload();
    }
  });
};

// 左侧树点击新建
const addOrg = data => {
  parentId.value = data ? data.orgId : null;
  parentName.value = data ? data.orgName : null;
  current.value = null;
  showEdit.value = true;
};
// 左侧树点击编辑
const editOrg = data => {
  current.value = data;
  showEdit.value = true;
};
// 切换左侧树宽度
const changeWidth = val => {
  width300.value = val;
};
</script>

<style scoped lang="less">
.table-header {
  width: 100%;
  height: 40px;
  line-height: 40px;
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
}
.table-content {
  width: 100%;
  height: calc(100% - 50px);
}
:deep(.ant-table-body) {
  min-height: calc(100vh - 240px) !important;
}
.search-input {
  width: 300px;
  border-radius: 5px;
}
.org-type {
  display: flex;
  justify-content: center;
  :deep(.ant-tag) {
    line-height: 24px;
  }
}
.icon-org-tree-company {
  color: var(--primary-color);
  margin-right: 5px;
  font-weight: 500;
}
.icon-org-tree-dept {
  margin-right: 5px;
}
</style>
